<?php

include './common/functions.php';
include_once './common/Db.class.php';

$data = $_POST;

foreach ($data as $k => $v) {
	switch ($k) {
		case 'username':
			checkUsername();
			break;
		case 'phone':
			checkPhone();
			break;
		case 'password':
			checkPassword();
			break;
		case 'repassword':
			checkrePassword();
			break;
	}
}

function checkUsername($value='')
{
	$username = $_POST['username'];

	$preg = "/[a-z]\w{5,7}/i";

	$c  = preg_match($preg,$username);

	if(!$c){
		exit(json_encode(['code'=>1,'msg'=>"用户名包含数字 字母下划线长度6-8,不能数字与下划线开头"]));
	}
	// 链接数据库
	$db = new Db('user');
	// 加where条件
	$db->where("username='{$username}'");

	$res = $db->select();

	if($res){
		exit(json_encode(['code'=>1,'msg'=>"用户名已存在"]));
	}
}

 function checkPhone()
{
	$phone = $_POST['phone'];

	$preg = "/1\d{10}/";

	$c  = preg_match($preg,$phone);

	if(!$c){
		exit(json_encode(['code'=>1,'msg'=>"手机格式不正确"]));
	}
}

 function checkPassword()
{
	$password = $_POST['password'];

	$preg = "/\w{6,}/";

	$c  = preg_match($preg,$password);

	if(!$c){
		exit(json_encode(['code'=>1,'msg'=>"密码必须6位已上"]));
	}
}

 function checkrePassword()
{
	if($_POST['repassword']!=$_POST['password']){
		exit(json_encode(['code'=>1,'msg'=>"两次密码不一致"]));
	}
}


exit(json_encode(['code'=>0,'msg'=>"数据正确"]));
